package java67_adv8.bank;

import java.util.Random;

public class Taker extends Thread {
	private Account ac;

	public Taker(Account ac, String name) {
		super(name);
		this.ac = ac;
	}

	@Override
	public void run() {
		Random r = new Random();
		int m = r.nextInt(200) + 1;
		while (true) {

			System.out.println(super.getName() + "计划取款" + m + "，元。" + "当前余额是：" + ac.getMoney() + "元");

			boolean f = ac.takeMoney(m);
			System.out.println(
					super.getName() + "取款：" + m + "元结果：" + (f ? "成功" : "失败") + ",现在余额是：" + ac.getMoney() + "元");

			if (f == false) {
				try {
					System.out.println(super.getName() + "取款：" + m + "元失败，等着。。。");
					synchronized (ac) {
						// wait的时候释放锁
						ac.wait(); // 注意死锁的问题
					}
					System.out.println(super.getName() + "计划取款：" + m + "元。被喊醒，继续取款：");

				} catch (InterruptedException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}

			}
			System.out.println(super.getName() + "取款成功....余额是：" + ac.getMoney() + "元");
			m = r.nextInt(200) + 1;
		}

	}
}
